package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016092400583187";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCEhZVrLCkWTrzTAh7OKlGwHAyfDDXZWIK0OT/pL5R4KIY/Ffj3IDt7MTDf6amB8k73PZikLCeCAiu9b5V3EIOvuVlS+bDo0uuNgIhSS0M37Br7GY0+DqXVt/uZbWkVOG9lv11BAEmxCC5i42S4qv3bdojIqG/FF6Yv11z6HDaavjnGSc0ueNdZ65z/xa0gISQ20OMEacIB8conEArSdcqBw7Ze3NXUhdX1HZ4Xv12PZyaX6ZXZ/+czqqwz5ZZ4M8tBbCNoAiTDvzW0+aWKF+X2XL+A2VmRCVhA6cxe+6NT3/BDj7pDVB/LZIL8Ae74qiqKAJqtz1liYwKv/wWQybIzAgMBAAECggEAD0TQ7XM+HKrfyLQi1oA7vz3MGnsurScskBVUjsqDocCRjy057UdScyQ8zF/Avw8rGAOJ1cicmLclUQNNRlWpxpJlf1DzmC/NIhWgH1M6oHPcu/e6tV68rr4vCAN6Kw7AjwmYdgfRdctKhOxMS5av/xxOMEhv8FaEOFjpBow/ol6C3YUL3uTfxVI1I2MBXqNedRX0StnUbRseVbckEGN/ap4WqGtci2i55JQNZQ/+WlfY1SgBaoEIBEjK/kg0nxjUG+WqQME4zPur+0Z2nw23AI5xMoFZPbwX73EoSm4aoSNKVwKqljnu1xnXdwOVWyDQo8jKlKwdDGMTJwlT8o1yWQKBgQDqUqKmjnETdGC+m+CRNlAC6gPWNFJqry4eRzUMN8kOSW2Csgl7PwyUb5K04huzBDdx9jvmmuAzmJ6dBVvdJyDYUg6znDy01wl2zj1iae6BeDMvPCcW3Q2ibrv/5F1pqumsn+w2qqIcpzkj16UHpl9m2pQOpNPkpQVGFtYY3DvFnwKBgQCQyAo8SoCSfmCCMDjkHxZJ3cFf6ks8pNvC5mqdQFayUQjk5tUm49BXjFM1qAxPXh9tIWVRDB5t9WeNtFh3Zj99gUpPeO48wiVa2yAksoI3SowX6ksAUY1VfCoUGH1wcFAogbxYNFv/pe0bYBBoBreITRde7bwZ5awitbCR/EeC7QKBgFhicxeAAwbjjtpW0Y30fpmay3ewpKW5Dhw9n7TMFfpdmAEERq9GSOh0bVHtN6V+oatKxa1mHctYdfukWdJBQBJYYVM+S2eq9NbrVBwa+Ou8+xGGDviAyPPL8P3ElbeV0C5c5Cx6kEteiB8M6CLKjpGtQMuFQKet1x9o3bji2WjhAoGAAcYhsflrt9/KiO3UVJ8447DO5MD3KQgQFURDfCfEqCysAjn91Jsn+1qhzl9/vx01H14wYpzWZpyCJ09VYikjkAMciPq/WIk8ce+LZke1pr3IF6bsQlW9eJWr4lEmgFEv+j8UsrP5iJlc3WAA83VZnFbteWFKU7rnsAkv36oKqkUCgYBbu580nYBLBfDZSHQvwbjwpk1/eMGRqNK3o8hJtQeUgGzdomAzG6skf5znZLyiCxxv61IGU8TNoo/jESztK4QGQ4rdhPasfTfLTLeZv7cOkVyjB/wB43vdpsyJyyhvPREz2ppy8Xv2bFT39NmjwtNIB8eJojcV5oUfRpFOJdlmeg==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApnR/jkPCrDooICQcS5dxgN2wL1ReRsbG5PUUq4BxZePGgUA95GuTSGmXbf4F4LFK/fCLgv2mgcER1gVJUI7xPCMlhL2M7/082jFc+NtBB+peI3KmfrDTHo9RUpyjsr1vFGwHBLXUXhTYafw/Tzu5j9LmGdrrc2Xyu20ZWp3LKL9k2j3GPd7aNllsFjvDRNeqvlRWFzV37E/SrrhOb1Air1fn+cXjWEVGk4ag5xCckCvYE7xIp+WETFP7p5zz4g63/gJt3OBv68no8GvmiXQrtZclRtKqir/HyMOdMztRs3jDYem0w7UnQ9jrwDUzKBPLvwAC221/DwStA+mVbTo3QQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://d2e0ax21l3.54http.tech/payed/notify";;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
